package site.zhangzhuo.v2;


import java.io.Serial;
import java.io.Serializable;
import java.util.AbstractMap;
import java.util.HashMap;

/*
字
通过读句子来建立字之间的关联
 */
public class Zi implements Serializable {
    @Serial
    private static final long serialVersionUID= 21L;
    private final Character ming;//字名
    //前后字及其和本字之间的连接值
    private final HashMap<Character,HashMap.SimpleEntry<Zi,Integer>> qian=new HashMap<>();//字前
    private final HashMap<Character,HashMap.SimpleEntry<Zi,Integer>> hou=new HashMap<>();//字后

    public Zi(Character danzi){
        ming=danzi;
    }
    public Character getMing(){
        return ming;
    }
    //添加前字
    public void addQian(Zi zi){
        if(qian.containsKey(zi.getMing())){
            qian.get(zi.getMing()).setValue(qian.get(zi.getMing()).getValue()+1);
        }
        else qian.putIfAbsent(zi.getMing(),new AbstractMap.SimpleEntry<Zi,Integer>(zi,1));
    }
    //添加后字
    public void addHou(Zi zi){
        if(hou.containsKey(zi.getMing())){
            hou.get(zi.getMing()).setValue(hou.get(zi.getMing()).getValue()+1);
        }
        else hou.putIfAbsent(zi.getMing(),new AbstractMap.SimpleEntry<Zi,Integer>(zi,1));
    }

    @Override
    public String toString(){
        return String.valueOf(this.ming);
    }
}
